use "psuedo_data.dta", clear

*Variable Calculations*
	*Debt*
		gen debt = notes_payable + current_long_term_debt + current_bonds + bonds + long_term_notes
	*Leverage*
		gen lev = debt/assets
	*Return on Assets*
		gen roa = ni/assets
	*Return on Equity*
		gen roe = ni/shareholders_equity
	*Profit Margin*
		gen prof_margin = ni/revenue
	*SG&A To Revenue*
		gen sga_rev = sga/revenue
	*Sales Growth*
		gen sales_growth = (revenue - revenue[_n-1])/revenue[_n-1] if year-year[_n-1]==1
	*Gross Margin*
		gen gross_margin = gm/revenue
	
*These values represent the latitude and longitude for ownership financing banks. This values are simulated bank longitudes and latitudes to maintain the anonymity of the data provider. 
	local agrp "83.3244 15.1866 34.9841 61.30914 44.12314 22.41515 66.31901"

	local bgrp "-48.1221 -28.31215 -60.12314 55.12314 90.96981 21.11213 45.1241"

local n : word count `agrp'

forvalues i = 1/`n' {
	local a : word `i' of `agrp'
	local b : word `i' of `bgrp'
	geodist company_lat company_lng `a' `b', gen(dist`i')
	}
	
	egen bank_dist = rowmin(dist1-dist7)

***************************************
*Calculate Ownership Variables*
	gen managerial_ownership = (100-corporate_ownership)/100
	gen other_ownership=(100-corporate_ownership-president_ownership)/100
	replace president_ownership = president_ownership/100


*Calculate Changes in Ownership*
	sort company year month
	by company: gen delta_president_ownership = (president_ownership-president_ownership[_n-1]) if year-year[_n-1]==1
	by company: gen delta_managerial_ownership = (managerial_ownership-managerial_ownership[_n-1]) if year-year[_n-1]==1
	by company: gen delta_other_ownership = (other_ownership-other_ownership[_n-1]) if year-year[_n-1]==1
	
	*Generate Indicator for Ownership Change*
		*Manager Change Indicator*
			gen delta_president_ownership_ind = 1 if delta_president_ownership!=0
			replace delta_president_ownership_ind = 0 if delta_president_ownership_ind==.
		*Manager and Non-Manager Ownership Change*
			gen delta_managerial_ownership_ind = 1 if delta_managerial_ownership!=0
			replace delta_managerial_ownership_ind = 0 if delta_managerial_ownership_ind==.
		*Non-Manager Ownership Change*
			gen delta_other_ownership_ind = 1 if delta_other_ownership!=0
			replace delta_other_ownership_ind = 0 if delta_other_ownership_ind==.
	
*Calculate Manager Age*
	sort company year
	gen birth_year = year(birth_day)
	gen age = year-birth_year
	
*Generate Price Indicator*
	gen prc_ind = 1 if year>=2018
	replace prc_ind = 0 if year<2018
	
*Generate Variables*
		
	*Alternative Costs Variables*
		gen cost_red1 = (cogs+sga)/revenue
		gen cost_red2 = (sga)/revenue
		gen cost_red3 = (rent_warehouse+depreciation+other_warehouse+rent_office+professional_service+accounting_service)/revenue
		
	*Change in Employees*
		by company: gen delta_emp = (employees-employees[_n-1])/employees[_n-1] if year-year[_n-1]
		
	*Control Variables*
		*Natural Log of Assets and Sales*
			gen ln_assets = ln(assets)
			gen ln_sales = ln(revenue)
			gen capex = (capx)/assets
			gen ppe_assets = ppe/assets
		
	*Effort* This is calculated following Hilary, Hsu, Segal, and Wang (2016) and labeled HHSW in the paper
		*Purchases represent inventory purchases made during the year*
		gen effort = roe/((purchases+capex)/assets)
		
	*Ownership Groups*
		local cut managerial_ownership
		egen ownership_group = xtile(managerial_ownership), nq(3) by(year)

*Profit Sharing*
	*Employee Share of Profit Sharing*
		gen share_ratio = employee_share/profit_share
		

*Squared Ownership*
		gen president_ownership_2 = president_ownership^2
		gen other_ownership_2 = other_ownership^2
		gen managerial_ownership_2 = managerial_ownership^2
	*Lag Controls*
		sort company year
				foreach l in president_ownership other_ownership managerial_ownership president_ownership_2 managerial_ownership_2 other_ownership_2 delta_president_ownership ln_assets ln_sales ppe_assets capex lev ownership_group age prc_ind roa delta_president_ownership_ind delta_managerial_ownership_ind delta_other_ownership_ind sga_rev {
					by company: gen l_`l' = `l'[_n-1] if year-year[_n-1]==1
				}
*****************FIGURES*****************
	*Figure 1: Kernel Density Function* - Values were obtained from Stata and recorded in excel to create the presented graphs
		kdensity l_managerial_ownership, nograph gen(xvar_managerial_ownership dens_managerial_ownership)
		kdensity l_president_ownership, nograph gen(xvar_president_ownership dens_president_ownership)
		kdensity l_other_ownership, nograph gen(xvar_other_ownership dens_other_ownership)
		

	*Figure 2: Time Series Average Ownership* - Values were obtained from Stata and recorded in excel to create the presented graphs
		forval i=2015/2022 {
			*Calculate mean and confidence interval for the given year
			mean ownership if year == `i', level(95)
			
			*Display Values
			scalar mean = r(mean)
			scalar lower_95 = r(lb)
			scalar upper_95 = r(ub)
			}
				
		forval i=2015/2022 {
			*Calculate mean and confidence interval for the given year
			mean president_ownership if year == `i', level(95)
			
			*Display Values
			estadd scalar mean = r(mean)
			estadd scalar lower_95 = r(lb)
			estadd scalar upper_95 = r(ub)
		}

		forval i=2015/2022 {
			*Calculate mean and confidence interval for the given year
			mean other_ownership if year == `i', level(95)
			
			*Display Values
			scalar mean = r(mean)
			scalar lower_95 = r(lb)
			scalar upper_95 = r(ub)
			
			}

	*Figure 3: Time Series Pattern of Average Ownership* - Values were obtained from Stata and recorded in excel to create the presented graphs
		forval i=2015/2020 {
			reg roa `interest' `controls1' if year==`i', vce(cl company) 
		}
		
*****************TABLES*****************
	*Table 1 - Summary Stats*
		tabstat ownership president_ownership other_ownership roa prof_margin gross_margin share_ratio sales_growth delta_emp effort age ppe_assets capex lev sga_rev, statistics(count mean p50 p25 p75 sd) columns(statistics)
			
	*Table 2*
		*Panel A and B - Summarize Statistics by Ownership Groups* 
			tabstat ownership president_ownership other_ownership roa prof_margin gross_margin share_ratio sales_growth delta_emp effort age ppe_assets capex lev sga_rev if l_ownership_group==1, statistics(mean p50) columns(statistics)
			tabstat ownership president_ownership other_ownership roa prof_margin gross_margin share_ratio sales_growth delta_emp effort age ppe_assets capex lev sga_rev if l_ownership_group==2, statistics(mean p50) columns(statistics)
			tabstat ownership president_ownership other_ownership roa prof_margin gross_margin share_ratio sales_growth delta_emp effort age ppe_assets capex lev sga_rev if l_ownership_group==3, statistics(mean p50) columns(statistics)

		*Run t-tests and Median Equivalence tests for Table 2*
			foreach i in ownership president_ownership other_ownership roa prof_margin gross_margin share_ratio sales_growth delta_emp effort age ppe_assets capex lev sga_rev {
				di "`i'"
				ttest `i' if l_ownership_group==1 | l_ownership_group==3, by(l_ownership_group)
				ranksum `i' if l_ownership_group==1 | l_ownership_group==3, by(l_ownership_group)
			}
					
*Winsorize All Variables - Performed Winsorization after summary statistics to provide higher fidelity view of underlying data
	foreach i in roa roe prof_margin gross_margin share_ratio ownership president_ownership other_ownership l_president_ownership l_ownership l_other_ownership l_ln_assets l_ln_sales l_ppe_assets l_capex l_lev l_sga_rev age delta_president_ownership delta_managerial_ownership delta_other_ownership cost_red1 cost_red2 cost_red3 delta_emp sales_growth effort {
			winsor2 `i', replace cuts(1 99)
		}
		
*Generate Interaction Terms using lagged ownership and price indicator*
	gen interaction = l_ownership*l_prc_ind
	gen interaction2 = l_president_ownership*l_prc_ind
	gen interaction3 = l_other_ownership*l_prc_ind
	
*Set Local Variables for regression analysis*
	local interest l_managerial_ownership
	local interest_both l_president_ownership l_other_ownership
	local indicator l_prc_ind
	local interest2 l_managerial_ownership l_managerial_ownership_2
	local interest4 l_president_ownership l_president_ownership_2 l_other_ownership l_other_ownership_2
	
	local controls1 l_ln_assets l_ln_sales l_ppe_assets l_capex l_lev l_sga_rev age
	
*Table 3*
	reghdfe roa `interest' `controls1', a(year) vce(cl company) 
	reghdfe roa `interest' `controls1', a(year company) vce(cl company)
	reghdfe roa `interest_both' `controls1', a(year) vce(cl company) 
	reghdfe roa `interest_both' `controls1', a(year company) vce(cl company) 
	

	*Table 4*
		local interest2 l_managerial_ownership l_managerial_ownership_2
		local interest4 l_president_ownership l_president_ownership_2 l_other_ownership l_other_ownership_2
		reghdfe roa `interest2' `controls1', a(year) vce(cl company) 
		reghdfe roa `interest2' `controls1', a(year company) vce(cl company)
		reghdfe roa `interest4' `controls1', a(year) vce(cl company) 
		reghdfe roa `interest4' `controls1', a(year company) vce(cl company) 
	
	*Table 5*
		forval i=1/3 {
			display `i'
			reghdfe roa `interest' `controls1' if l_ownership_group==`i', a(year) vce(cl company) 
			reghdfe roa `interest_both' `controls1' if l_ownership_group==`i', a(year) vce(cl company)
		}
	
		
	*Table 6*
		foreach i in share_ratio prof_margin gross_margin sales_growth delta_emp effort {
			reghdfe `i' `interest' `controls1', a(year) vce(cl company)
			reghdfe `i' `interest' `controls1', a(year company) vce(cl company)
			reghdfe `i' `interest_both' `controls1', a(year) vce(cl company)
			reghdfe `i' `interest_both' `controls1', a(year company) vce(cl company)
		}

	*Table 7*
		reghdfe delta_managerial_ownership `indicator' `controls1', a(company) vce(cl company) 
		reghdfe delta_president_ownership `indicator' `controls1', a(company) vce(cl company)
		reghdfe delta_other_ownership `indicator' `controls1', a(company) vce(cl company)

	*Table 8*
		reghdfe roa `interest' interaction `controls1', a(year) vce(cl company)
		reghdfe roa `interest_both' interaction2 interaction3 `controls1', a(year) vce(cl company)
	
	*Table 9*
		foreach i in share_ratio prof_margin gross_margin sales_growth delta_emp effort {
			reghdfe `i' `interest' interaction `controls1', a(year) vce(cl company)
			reghdfe `i' `interest_both' interaction2 interaction3 `controls1', a(year) vce(cl company)
		}

	*Table 10*

		*Bank Distance Calculation*
			egen med_bank = median(bank_dist)

			gen bank1 = 1 if bank_dist<=med_bank
			replace bank1 = 0 if bank_dist>med_bank


		*SECOND STAGE RESULTS*
		foreach i in roa share_ratio prof_margin gross_margin sales_growth delta_emp effort {
			ivreghdfe `i' l_ln_assets l_ln_sales l_ppe_assets l_capex l_lev age l_sga_rev (l_ownership = bank1), a(year) cl(company) first
		}

*****************INTERNET APPENDIX*****************

	*Table IA1*
		*Drop Retiring*
			reghdfe roa `interest2' `controls1' if age<59, a(year) vce(cl company)
			reghdfe roa `interest2' `controls1' if age<59, a(company year) vce(cl company) 
			reghdfe roa `interest4' `controls1' if age<59, a(year) vce(cl company)
			reghdfe roa `interest4' `controls1' if age<59, a(company year) vce(cl company) 
		
	*Table IA2*	
		foreach i in cost_red1 cost_red2 cost_red3 { 
			reghdfe `i' `interest' `controls1', a(year company) vce(cl company)
			reghdfe `i' `interest_both' `controls1', a(year company) vce(cl company)
		}
		
	*Table IA3*
		reghdfe delta_managerial_ownership_ind `indicator' `controls1', a(company) vce(cl company) 
		reghdfe delta_president_ownership_ind `indicator' `controls1', a(company) vce(cl company)
		reghdfe delta_other_ownership_ind `indicator' `controls1', a(company) vce(cl company)

		
	*Table IA4*
		eststo clear
		foreach i in cost_red1 cost_red2 cost_red3 { 
			reghdfe `i' `interest' interaction `controls1', a(year) vce(cl company)
			reghdfe `i' `interest_both' interaction2 interaction3 `controls1', a(year) vce(cl company)
		}

		
	*Table IA5*
		gen shock = 0
		replace shock = 1 if company==1 & year==2015
		replace shock = 1 if company==2 & year==2013
		replace shock = 1 if company==3 & year==2015
		replace shock = 1 if company==4 & year==2020
		replace shock = 1 if company==5 & year==2016

		gen after1=0

		replace after1 = 1 if company==1 & year==2019
		replace after1 = 1 if company==2 & year==2018
		replace after1 = 1 if company==3 & year==2016
		replace after1 = 1 if company==4 & year==2021
		replace after1 = 1 if company==5 & year==2017
		gen after2=0
		replace after2 = 1 if company==1 & year==2020
		replace after2 = 1 if company==2 & year==2019
		replace after2 = 1 if company==3 & year==2017
		replace after2 = 1 if company==4 & year==2022
		replace after2 = 1 if company==5 & year==2018
		
		gen before1=0

		replace before1 = 1 if company==1 & year==2017
		replace before1 = 1 if company==2 & year==2016
		replace before1 = 1 if company==3 & year==2014
		replace before1 = 1 if company==4 & year==2019
		replace before1 = 1 if company==5 & year==2015

		gen before2=0
		replace before2 = 1 if company==1 & year==2016
		replace before2 = 1 if company==2 & year==2015
		replace before2 = 1 if company==3 & year==2013
		replace before2 = 1 if company==4 & year==2018
		replace before2 = 1 if company==5 & year==2014

		foreach i in ownership president_ownership other_ownership roa prof_margin gross_margin sales_growth delta_emp effort {
			sum `i' if before2==1
			sum `i' if before1==1
			sum `i' if shock==1
			sum `i' if after1==1
			sum `i' if after2==1
		}
